package lab03.test08;
/*将文件一和文件二的内容合并成文件三，
合并方法是：从文件一读取一个字节放入文件三，
再从文件二读取一个字节放入文件三，如此轮流直至某一个文件读完，
再将较长文件中的剩余部分读取放入至文件三。
*/
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test08 {
    public static void main(String[] args) {

        String path1 = "F:\\1.txt";
        String path2 = "F:\\2.txt";
        String path3 = "F:\\3.txt";

        FileInputStream file1 = null;
        FileInputStream file2 = null;
        FileInputStream file3 = null;

        FileOutputStream file3Write = null;
        try {
            file1 = new FileInputStream(path1);
            file2 = new FileInputStream(path2);
            file3 = new FileInputStream(path3);

            file3Write = new FileOutputStream(path3);

            int current = 0;
            int current2 = 0;

            StringBuffer file1Buffer = new StringBuffer();
            StringBuffer file2Buffer = new StringBuffer();



            if (file1.available() < file2.available()) {


                while (((current2 = file2.read()) != -1)) {
                    file3Write.write(current2);

                    while ((current = file1.read()) != -1) {
                        file3Write.write(current);

                        break;
                    }
                    continue;
                }
            } else {

                while (((current2 = file1.read()) != -1)) {
                    file3Write.write(current2);

                    while ((current = file2.read()) != -1) {

                        file3Write.write(current);
                        break;
                    }
                    continue;
                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                file1.close();
                file2.close();
                file3.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
